package com.ita.controller;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ita.service.IBookService;
import com.ita.vo.Book;

@Controller(value="bookController")
@Scope
@RequestMapping("/book")
public class BookController {

	private IBookService service;
	
	
	public IBookService getService() {
		return service;
	}

    @Resource
	public void setService(IBookService service) {
		this.service = service;
	}


	@RequestMapping(value="/showAllBook",method=RequestMethod.GET)
	@ResponseBody
	public List<Book> showAll(){
		System.out.println("showAllBook");
		List<Book> list=service.queryAll();
		System.out.println(list);
		return list;
	}
	
	
	@RequestMapping(value="/{bookid}",method=RequestMethod.DELETE)
	@ResponseBody
	public String deleteBook(@PathVariable String bookid){
		service.deleteById(bookid);
		return "success";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public String addBook(String bookname,String author,double price){
//		String bookid=String.valueOf(UUID.randomUUID());
		Book book=new Book(bookname, author, price);
		service.insert(book);
		return "success";
	}
	
	@RequestMapping(value="/{bookid}",method=RequestMethod.PUT)
	@ResponseBody
	public String updateBook(String bookid,String bookname,String author,double price){
		Book book=new Book(bookid,bookname, author, price);
		System.out.println("update:"+book);
		service.update(book);
		return "success";
	}
}
